package com.control.demo;


import com.control.R;
import com.control.dao.DatabaseHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SQLiteActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqlite);
		Button createDataBase = (Button)findViewById(R.id.createDataBase);
		Button updateDataBase = (Button)findViewById(R.id.updateDataBase);
		Button inserDate = (Button)findViewById(R.id.insertData);
		Button updateData = (Button)findViewById(R.id.updateData);
		Button queryData = (Button)findViewById(R.id.queryData);
		createDataBase.setOnClickListener(new CreateListener());
		updateDataBase.setOnClickListener(new UpdateListener());
		inserDate.setOnClickListener(new InsertListener());
		updateData.setOnClickListener(new UpdateRecordListener());
		queryData.setOnClickListener(new QueryListener());
	}
	class CreateListener implements OnClickListener{
		public void onClick(View v){
			DatabaseHelper  dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_database");
			SQLiteDatabase db = dbHelper.getReadableDatabase();
		}
	}
	class UpdateListener implements OnClickListener{

		public void onClick(View v) {
			//DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_database",2);
			//SQLiteDatabase db = dbHelper.getReadableDatabase();
		}
	}
	class InsertListener implements OnClickListener{

		public void onClick(View v) {
			ContentValues values = new ContentValues();
			values.put("id", 1);
			values.put("name","zhangsan");
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_database");
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			db.insert("user", null, values);
		}
		
	}
	
	class UpdateRecordListener implements OnClickListener{

		public void onClick(View v) {
			DatabaseHelper dbHelper = new  DatabaseHelper(SQLiteActivity.this,"test_database");
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("name", "zhangsanfeng");
			db.update("user", values, "id=?", new String[]{"1"});
		}
	}
	class QueryListener implements OnClickListener{

		public void onClick(View v) {
			DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_database");
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?",new String[]{"1"},"","","");//, "groupBy", "having", "orderBy");
			while(cursor.moveToNext()){
				String name = cursor.getString(cursor.getColumnIndex("name"));
				System.out.println("query--->" + name);
			}
		}
	}
	
}
